/*********************************公用部分*******************************************/
var xianxiapeixunid = "", h5 = "", p = "";
//选择卡
$("#queding").click(function () {
    $(this).parent().parent().hide();
})
//字符串拼接
function joint(val) {
    var le = val.length;
    var str;
    if (le == 3) {
        str = val.substring(0, 1) + ":";
        str = str + val.substring(1, 3);
    } else {
        str = val.substring(0, 2) + ":";
        str = str + val.substring(2, 4);
    }
    return str;
}
//计算课程时间
function danrishijian(time) {
    var miao = "";
    var arr = [];
    arr = time.split(":");
    miao = arr[0] * 3600 + arr[1] * 60;
    return miao;
}
//点击空白处关闭窗口
$(".yizhou>.yycg").click(function(){
	$(this).hide();
})
/***********************************单日排课*******************************************/
function danri(json) {
    var txt = "";
    var danr = new Date(new Date()) / 1000;
    JSON.stringify(json.sort(function (x, y) {
        var x = x["start"], y = y["start"];
        if (x == y) {
            return 0;
        }
        return x - y;
    }));
    //遍历填写单日课程
    $.each(json, function (key, va) {
        var style = "";
        var danrinumber = "";
        var start = joint(va["start"]);
        var time = parseInt(danrishijian(start)) + parseInt(va["time"]);
        var residue = va["number"] - va["apply"];
        if (residue < 0) {
            residue = 0;
        }
        if (danr >= time) {
            style = "display:none";
            danrinumber = "已经开课";
        } else {
            danrinumber = '剩余<span>' + residue + '</span>人';
        }
        txt += "<li><h1>" + start + "</h1><h2>" + va["title"] + "</h2><h3>" + va["name"] + "</h3><h4>" + danrinumber + "</h4><h5 style='" + style + "' va='" + va["id"] + "' yuyue=''>立即预约</h5></li>";
    })
    $("#danri").html(txt);
    //点击报名
    $("#danri>li>h5").click(function () {
        h5 = $(this);
        //查询该用户是否办过卡
        new Ajax("/card").keyvalue("type", "1").getJson(function (json) {
            if (json.length == 0) {
                alert("请先办理会员卡");
            } else {
                var tx = "";
                var ht = $("#card select");
                ht.empty();
                $.each(json, function (key, val) {
                    tx += "<option>" + val["cardnumber"] + "</option>"
                })
                ht.html(tx);
                $(".zhou_1>.yycg").show();//选择卡
                //确认约课
            }
        })
    })
}
$("#queding").click(function () {
    var query = $("#card>select").val();
    new Ajax("/cardquery").keyvalue("cardnumber", query).getJson(function (json) {
        var id = json[0].id;//获取卡id
        var typeid = json[0].typeid;  //获取卡类型
        var number = parseInt(h5.siblings().find("span").text());
        var xianxiapeixunid = h5.attr("va");
        if (confirm("是否预约")) {
            new Ajax("/xianxiadingdan").keyvalue("cardid",id).keyvalue("xianxiapeixunid", xianxiapeixunid).get(function (js) {
                if (js == "1") {
                    new Ajax("/keshi").keyvalue("id", id).keyvalue("typeid", typeid).get();
                    h5.text("已预约").css({"background": "#e29004"});
                    if (number > 1) {
                        number--;
                        h5.siblings().find("span").text(number);
                    } else {
                        h5.text("正在排队").css({"background": "#e29004"});
                    }
                } else {
                    alert("您已经预约过该课程可到我的课程里面查看约课信息");
                }
            })
        }
    });
})


//时间填写
function shijianpaixu(danriStamp) {
    var txt = "";
//对于时间进行排序（只排列四天的时间）
    for (var i = 0; i < 4; i++) {
        if (i != 0) {
            danriStamp += 86400;
        }
        //判断今日日期为周几
        var dy = new Date(danriStamp * 1000).getDay();
        //获取前端的四个标签用于显示四天的时间
        //显示日期
        var z = new Date(danriStamp * 1000).toLocaleString().substr(5, 5);
        //位li标签添加时间戳
        $(".zhou>ul>li").eq(i).attr("day", danriStamp);
        $(".zhou>ul>li").eq(i).find("p").first().text(z);
        //显示周几
        zhou(dy);
        $(".zhou>ul>li").eq(i).find("p").last().text(txt);
    }
//清空课程
    $("#danri").empty();

//将函数计算的周几进行解析
    function zhou(e) {
        switch (e) {
            case 1:
                txt = "周一";
                break;
            case 2:
                txt = "周二";
                break;
            case 3:
                txt = "周三";
                break;
            case 4:
                txt = "周四";
                break;
            case 5:
                txt = "周五";
                break;
            case 6:
                txt = "周六";
                break;
            case 0:
                txt = "周日";
                break;
        }
    }

}
/*************************************本周排课*******************************************************/
function kecheng(jiaoshi, shijian) {
    var danr = new Date(new Date()) / 1000//当日时间
    var monday = shijian;//初始化周一时间
    var week = shijian;//初始化周末时间
    //判断当天为周几
    var dy = new Date(shijian * 1000).getDay();//周几
    if (dy == 0) {
        dy == 7;
    }
//计算本周，周一，以及周末的时间戳
    for (var i = dy; i >= 1; i--) {
        if (i != dy) {
            monday -= 86400;
        }
        var z = new Date(monday * 1000).toLocaleString().substr(5, 5);
        $(".riqi>li").eq(i - 1).attr("day", monday);
        $(".riqi li").eq(i - 1).find("span").text(z);
    }
//周末时间戳
    for (var i = dy; i <= 7; i++) {
        if (i != dy) {
            week += 86400;
        }
        var z = new Date(week * 1000).toLocaleString().substr(5, 5);
        $(".riqi>li").eq(i - 1).attr("day", week);
        $(".riqi li").eq(i - 1).find("span").text(z);
    }
    $("#tab>tbody>tr>td").empty();
    new Ajax("/tappcourse").keyvalue("monday", monday).keyvalue("week", week).keyvalue("classroomid", jiaoshi).getJson(function (json) {
        var one = [], two = [], three = [], js = {};//定义存储空间
        //以下为循环遍历出每天的课程
        for (var i = 0; i <= 6; i++) {
            var shangwu = $("#tab>tbody>tr").eq(0).find("td").eq(i), xiawu = $("#tab>tbody>tr").eq(1).find("td").eq(i), wanshang = $("#tab>tbody>tr").eq(2).find("td").eq(i);
            var shijian = $(".riqi>li").eq(i).attr("day");
            //区分时间段
            $.each(json, function (key, val) {
                if (shijian == val["time"] && val["start"] >= 600 && val["start"] <= 1200) {
                    js = val;
                    one.push(js);
                }
                if (shijian == val["time"] && val["start"] > 1200 && val["start"] <= 1800) {
                    js = val;
                    two.push(js);
                }
                if (shijian == val["time"] && val["start"] > 1800 && val["start"] <= 2300) {
                    js = val;
                    three.push(js);
                }
            })
            //按时间进行排序
            JSON.stringify(one.sort(function (x, y) {
                var x = x["start"], y = y["start"];
                if (x == y) {
                    return 0;
                }
                return x - y;
            }));
            JSON.stringify(two.sort(function (x, y) {
                var x = x["start"], y = y["start"];
                if (x == y) {
                    return 0;
                }
                return x - y;
            }));
            JSON.stringify(three.sort(function (x, y) {
                var x = x["start"], y = y["start"];
                if (x == y) {
                    return 0;
                }
                return x - y;
            }));
            bianli(one, shangwu);
            bianli(two, xiawu);
            bianli(three, wanshang);
            one = [];
            two = [];
            three = [];
        }
        //点击是否约课 
        $("#tab>tbody>tr>td>div>p").click(function () {
            p = $(this);
            xianxiapeixunid = $(this).attr("va");
            //查询该用户是否办过卡
            new Ajax("/card").keyvalue("type", "1").getJson(function (json) {
                if (json.length == 0) {
                    alert("请先办理会员卡");
                } else {
                    var tx = "";
                    var ht = $("#card_1 select");
                    ht.empty();
                    $.each(json, function (key, val) {
                        tx += "<option>" + val["cardnumber"] + "</option>"
                    })

                    ht.html(tx);
                    $(".yizhou>.yycg").show();//选择卡
                    //确认约课
                }
            })
        })
    })
//遍历填写数据
//ke代表json数据 ht代表相应的标签
    function bianli(ke, ht) {
        var val = "";
        $.each(ke, function (key, va) {
            var style = "";
            var danrinumber = "";
            var start = joint(va["start"]);
            var time = parseInt(danrishijian(start)) + parseInt(va["time"]);
            var residue = va["number"] - va["apply"];
            if (residue < 0) {
                residue = 0;
            }
            if (danr >= time) {
                style = "display:none";
                danrinumber = "已经开课";
            } else {
                danrinumber = '剩余<span>' + residue + '</span>人';
            }
            val += "<div><h1>" + start + "</h1><h2>" + va["classroomname"] + "</h2><h3>" + va["name"] + "</h3><h4>" + danrinumber + "</h4><p style='" + style + "' va='" + va["id"] + "'>预约</p></div>";
        })
        ht.html(val);
    }
}
//点击确定进行约课
$("#queren").click(function () {
    var query = $("#card_1>select").val();
    new Ajax("/cardquery").keyvalue("cardnumber", query).getJson(function (json) {
        var id = json[0].id;//获取卡id
        var typeid = json[0].typeid;  //获取卡类型
        var number = parseInt(p.siblings().find("span").text());
        var xianxiapeixunid = p.attr("va");
        if (confirm("是否预约")) {
            new Ajax("/xianxiadingdan").keyvalue("xianxiapeixunid", xianxiapeixunid).get(function (js) {
                if (js == "1") {
                    new Ajax("/keshi").keyvalue("id", id).keyvalue("typeid", typeid).get();
                    p.text("已预约").css({"background": "#e29004"});
                    if (number > 1) {
                        number--;
                        p.siblings().find("span").text(number);
                    } else {
                        p.text("正在排队").css({"background": "#e29004"});
                    }
                    $(".yizhou>.yycg").hide();//点击确认过后
                } else {
                    alert("您已经预约过该课程可到我的课程里面查看约课信息");
                    $(".yizhou>.yycg").hide();//点击确认过后
                }
            })
        } else {
            $(".yizhou>.yycg").hide();//取消预约
        }
    })
})




